﻿package com.jdplat.jdm.core.web.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** 
* @author: qirp 
* @time: 2016年11月29日 下午3:39:07 
*/
public class ActionRequest {
	
	protected Logger logger = LoggerFactory.getLogger(ActionRequest.class);
	
	/**
	 * 组装返回数据
	 * @param data
	 * @param response
	 */
	public static void renderData(String data,HttpServletResponse response){
		response.setContentType("application/json;charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out.write(data);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/***************工具****************/
	/**
	 * 
	 * @param name
	 * @param request
	 * @return
	 */
	public static String getPara(String name, HttpServletRequest request) {
		return request.getParameter(name);
	}

	public static String getPara(String name, String defaultValue, HttpServletRequest request) {
		String result = request.getParameter(name);
		return result != null && !"".equals(result) ? result : defaultValue;
	}

	public static String[] getParaValues(String name, HttpServletRequest request) {
		return request.getParameterValues(name);
	}

	public static Object getAttr(HttpServletRequest request, String key) {
		return request.getAttribute(key);
	}

	public static Enumeration<String> getAttrs(HttpServletRequest request) {
		return request.getAttributeNames();
	}

	public static void setAttr(HttpServletRequest request, String key, Object value) {
		request.setAttribute(key, value);
	}

	public static void setAttrs(HttpServletRequest request, Map<String, Object> map) {
		for (Map.Entry<String, Object> m : map.entrySet()) {
			request.setAttribute(m.getKey(), m.getValue());
		}
	}

}
